﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using game_for_test_helper.src.helper;
using Microsoft.Xna.Framework.Graphics;
namespace game_for_test_helper.src.graphics
{
    class LineManger : DrawableGameComponent
    {
        private short[] lineListIndices;
        private VertexPositionColor[] pointList;
        private int NumberOfPoints;
        private const int maxPoint = 8000;
        private BasicEffect basicEffect;

        private static LineManger GloabLineManager;


        private Matrix worldMatrix;
        private Matrix projectionMatrix;

        public LineManger(Game game)
            : base(game)
        {


        }
        public override void Initialize()
        {
            base.Initialize();
            NumberOfPoints = 0;
            pointList = new VertexPositionColor[maxPoint];
            lineListIndices = new short[maxPoint];
            basicEffect = new BasicEffect(GraphicsDevice, null);
            basicEffect.VertexColorEnabled = true;

            worldMatrix = GameStaticHolder.WorldMatrix;
            projectionMatrix = GameStaticHolder.ProjectionMatrix;
            GloabLineManager1=this;
        }
        protected override void LoadContent()
        {
            base.LoadContent();
        }
        protected override void UnloadContent()
        {
            base.UnloadContent();
        }
        public void drawline(Vector3 begin, Vector3 end)
        {
            Color color = Color.White;
           
            VertexPositionColor vpc = new VertexPositionColor(begin, color);
            VertexPositionColor vpc2 = new VertexPositionColor(end, color);
            pointList[NumberOfPoints * 2] = vpc;
            pointList[NumberOfPoints * 2 + 1] = vpc2;
            lineListIndices[NumberOfPoints * 2] = (short)(NumberOfPoints * 2);
            lineListIndices[(NumberOfPoints * 2) + 1] = (short)(NumberOfPoints * 2 + 1);
            NumberOfPoints++;
        }
        public void drawline(Vector3 begin, Vector3 end, Color color)
        {
          
            VertexPositionColor vpc = new VertexPositionColor(begin, color);
            VertexPositionColor vpc2 = new VertexPositionColor(end, color);
            pointList[NumberOfPoints * 2] = vpc;
            pointList[NumberOfPoints * 2 + 1] = vpc2;
            lineListIndices[NumberOfPoints * 2] = (short)(NumberOfPoints * 2);
            lineListIndices[(NumberOfPoints * 2) + 1] = (short)(NumberOfPoints * 2 + 1);
            NumberOfPoints++;
        }
        public override void Update(GameTime gameTime)
        {
            NumberOfPoints = 0;

            basicEffect.World = GameStaticHolder.WorldMatrix; ;
            basicEffect.View = SimpleCamera.ViewMatrix;
            basicEffect.Projection = GameStaticHolder.ProjectionMatrix;
            
            base.Update(gameTime);
        }
        internal static LineManger GloabLineManager1
        {
            get { return LineManger.GloabLineManager; }
            set { LineManger.GloabLineManager = value; }
        }
        public override void Draw(GameTime gameTime)
        {


            basicEffect.Begin(SaveStateMode.SaveState);
            GraphicsDevice.VertexDeclaration = GameStaticHolder.VertexDeclaration;

            foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
            {
                pass.Begin();
                if (NumberOfPoints > 0)
                    GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
                       PrimitiveType.LineList,
                       pointList,
                       0,  // vertex buffer offset to add to each element of the index buffer
                       NumberOfPoints*2,  // number of vertices in pointList
                       lineListIndices,  // the index buffer
                       0,  // first index element to read
                       NumberOfPoints   // number of primitives to draw
                   );
                //System.Console.WriteLine(NumberOfPoints);
                GraphicsDevice.RenderState.FillMode = FillMode.Solid;
                pass.End();
            }
            basicEffect.End();

            base.Draw(gameTime);
        }
    }
}
